﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using ToracTechnologies.ExpressionTreeLibrary.DynamicApi.Framework;

namespace ToracTechnologies.ExpressionTree.Testing.DynamicApi
{

    [TestClass]
    public class DynamicApiCombinerBuilderUnitTest
    {

        #region String Based

        [TestMethod]
        public void OrElse_Test1()
        {
            var lst = Jason.CreateDummyList(10);

            var leftFirst = ParameterBuilder.BuildParameterFromLinqPropertySelector<Jason>(x => x.Id);
            var firstExpression = ExpressionBuilder.BuildStatement<Jason, int>(leftFirst, ExpressionBuilder.DynamicUtilitiesEquations.Equal, 5);

            var leftSecond = ParameterBuilder.BuildParameterFromStringName<Jason>("Id");
            var secondExpression = ExpressionBuilder.BuildStatement<Jason, int>(leftSecond, ExpressionBuilder.DynamicUtilitiesEquations.Equal, 6);

            var sql = ExpressionCombiner.CombineExpressions<Jason>(firstExpression, ExpressionCombiner.CombineType.OrElse, secondExpression);

            var results = lst.AsQueryable().Where(sql).OrderBy(x => x.Id).ToArray();

            Assert.AreEqual(2, results.Count());

            Assert.AreEqual(5, results.ElementAt(0).Id);
            Assert.AreEqual(6, results.ElementAt(1).Id);
        }

        [TestMethod]
        public void AndAlso_Test1()
        {
            var lst = Jason.CreateDummyList(10);

            var leftFirst = ParameterBuilder.BuildParameterFromLinqPropertySelector<Jason>(x => x.Id);
            var firstExpression = ExpressionBuilder.BuildStatement<Jason, int>(leftFirst, ExpressionBuilder.DynamicUtilitiesEquations.Equal, 5);

            var leftSecond = ParameterBuilder.BuildParameterFromStringName<Jason>("txt");
            var secondExpression = ExpressionBuilder.BuildStatement<Jason, string>(leftSecond, ExpressionBuilder.DynamicUtilitiesEquations.Equal, "Test_2");

            var sql = ExpressionCombiner.CombineExpressions(firstExpression, ExpressionCombiner.CombineType.AndAlso, secondExpression);

            var results2 = lst.AsQueryable().Where(sql).OrderBy(x => x.Id).ToArray();

            Assert.AreEqual(0, results2.Count());

            var thirdExpression = ExpressionBuilder.BuildStatement<Jason, string>(leftSecond, ExpressionBuilder.DynamicUtilitiesEquations.Equal, "Test_5");
            var sql2 = ExpressionCombiner.CombineExpressions(firstExpression, ExpressionCombiner.CombineType.AndAlso, thirdExpression);

            var results3 = lst.AsQueryable().Where(sql2).OrderBy(x => x.Id).ToArray();

            Assert.AreEqual(1, results3.Count());
            Assert.AreEqual(5, results3.ElementAt(0).Id);
        }

        #endregion

    }
}


